<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>
<script type="text/javascript">

var detailValidator;

function selectIndicator() { 
	var currentIndicatorId = $("#indicatorId").val();
	$.openDialog("#dialog",
			{height:350,
			width:300,
			loadPage:ctx+"/biz/indicator/query.jsp?exclude="+currentIndicatorId,
			title:"选择上级指标项"},
			function(data) {
				$("#indicatorParent").data("id",data.data("id"));
				$("#indicatorParent").val(data.data("name"));
			});
}

function selectCoordinate() {
	$.openDialog("#dialog",
			{loadPage:ctx+"/biz/map/query.jsp",title:"坐标选择"},
			function(data) {
				$("#indicatorLongitude").val(data.x);
				$("#indicatorLatitude").val(data.y);
			} );
}

function selectDistrict() {
	$.openDialog("#dialog",
			{loadPage:ctx+"/biz/map/district/query.jsp",title:"区域选择"},
			function(data) {
				$("#indicatorDistrict").val(data.name);
				$("#indicatorDistrict").data("code",data.name);
			} );
}

function saveDetail() {
	if (!detailValidator.form()) {
		return;
	}
	doSaveDetail();
}

function doSaveDetail() {
	var data = {
			id : $("#indicatorId").val(),
			name : $("#indicatorName").val(),
			shortName : $("#indicatorShortName").val(),
			code : $("#indicatorCode").val(),
			boundProperties : $("#boundProperties").val(),
			parentId: $("#indicatorParent").data("id"),
			creatorId: $("#creatorId").val(),
			sortNum: $("#sortNum").val(),
			measureUnit: $("#measureUnit").val(),
			description : $("#indicatorDesc").val()
		};
	
	block("form");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: data,
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("form");
			$.alert(message);
		}  
	});
}

function saveSucceedCallback(data) {
	unblock("form");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$("#indicatorId").val(data.data.id);
	$.info("已保存");
}

$(document).ready(function() {
	$("#indicatorParent").data("id",$("#indicatorParentId").val());
	//$("#indicatorDistrict").data("code",$("#indicatorDistrictCode").val());

	var id = $("#indicatorId").val();
	var pid = $("#indicatorParentId").val();
	
	if(id != ""){
		$("#depthCg").show();
		$("#creatorCg").show();
		$("#createDateTimeCg").show();
	}
	
	var depth = $("#depth").val();
// 	if(depth == "3"){
		$("#boundPropertiesCg").show();
		//$("#boundPropertiesCg").data("readonly","readonly");
// 	}
	
	if (id != "" && pid == "") {
		$("#indicatorRequired").hide();
		$("#selectIndicatorBtn").hide();
		detailValidator = $("#detailForm").validate({
			rules: {
				indicatorName:{required:true, maxlength:50},
				indicatorCode:{required:true, maxlength:50},
				indicatorShortName:{required:true, maxlength:30},
				sortNum:{required:true, number:true, maxlength:11},
				indicatorDesc:{maxlength:1000}
			},
			messages:{
				indicatorName:{required:"请输入名称"},
				indicatorCode:{required:"请输入编码"},
				sortNum:{required:"请输入排序标识（升序）"},
				indicatorShortName:{required:"请输入简称"}
			}
		});
	} else {
		detailValidator = $("#detailForm").validate({
			rules: {
				indicatorName:{required:true, maxlength:50},
				indicatorCode:{required:true, maxlength:50},
				indicatorShortName:{required:true, maxlength:30},
				//indicatorParent:{required:true},
				sortNum:{required:true, number:true, maxlength:11},
				indicatorDesc:{maxlength:1000}
			},
			messages:{
				indicatorName:{required:"请输入名称"},
				indicatorCode:{required:"请输入编码"},
				//indicatorParent:{required:"请选择上级"},
				sortNum:{required:"请输入排序标识（升序）"},
				indicatorShortName:{required:"请输入简称"}
			}
		});
	}
	$().maxlength();

});

</script>

<div class="border bg">

<div class="page-header">
<h2>指标项 <small> - ${(data.name==null)?"新增":data.name}</small></h2>
<div class="cms_toolbar"><a href="./" class="btn"><i class="icon-list"></i>返回</a></div>
</div> 

<form id="detailForm" class="form-horizontal">
<input type="hidden" id="indicatorId" value="${data.id}">
<input type="hidden" id="indicatorParentId" value="${data.parentId}">
<input type="hidden" id="creatorId" value="${data.creatorId}">
<%-- <input type="hidden" id="indicatorDistrictCode" value="${data.districtCode}"> --%>

<fieldset>
	<div class="control-group">
		<label for="indicatorName" class="control-label">名称：</label>
		<div class="controls">
			<input type="text" id="indicatorName" name="indicatorName" maxlength="50" value="${data.name}"> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="indicatorShortName" class="control-label">简称：</label>
		<div class="controls">
			<input type="text" id="indicatorShortName" name="indicatorShortName" maxlength="30" value='${data.shortName}'> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="indicatorCode" class="control-label">编码：</label>
		<div class="controls">
			<input type="text" id="indicatorCode" name="indicatorCode" maxlength="50" value='${data.code}'> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group" style="display: none;" id="depthCg">
		<label for="depth" class="control-label">层级：</label>
		<div class="controls">
			<input type="text" id="depth" readonly="readonly" name="depth" maxlength="50" value='${data.depth}'>
		</div>
	</div>
	
	<div class="control-group" style="display: none;" id="boundPropertiesCg">
		<label for="boundProperties" class="control-label">绑定属性：</label>
		<div class="controls">
			<input type="text" id="boundProperties" name="boundProperties" maxlength="50" value='${data.boundProperties}'>
		</div>
	</div>
	
	<div class="control-group">
		<label for="indicatorParent" class="control-label">上级：</label>
		<div class="controls">
			<input type="text" id="indicatorParent" readonly="readonly" name="indicatorParent" value="${data.parentName}">
			<span class="cms_color_required" id="indicatorRequired">*</span>
			<input type="button" class="btn" value="选择" onclick="selectIndicator()" id="selectIndicatorBtn">
		</div>
	</div>
	
	<div class="control-group">
		<label for="indicatorDesc" class="control-label">描述：</label>
		<div class="controls">
			<textarea id="indicatorDesc" name="indicatorDesc" maxlength="1000">${data.description}</textarea>
		</div>
	</div>
	<div class="control-group">
		<label for="sortNum" class="control-label">排序标识：</label>
		<div class="controls">
			<input type="text" id="sortNum" name="sortNum" maxlength="11" value="${data.sortNum}"/>
		</div>
	</div>
	<div class="control-group">
		<label for="measureUnit" class="control-label">计量单位：</label>
		<div class="controls">
			<input type="text" id="measureUnit" name="measureUnit" maxlength="8" value="${data.measureUnit}"/>
		</div>
	</div>	
	<div class="control-group" style="display: none;" id="creatorCg">
		<label for="creator"  class="control-label">创建人：</label>
		<div class="controls">
		<input type="text" id="creator" name="creator" readonly="readonly" value='${data.creatorName}'/>
		</div>
	</div>
	<div class="control-group" style="display: none;" id="createDateTimeCg">
		<label for="createDateTime"  class="control-label">创建时间：</label>
		<div class="controls">
		<input type="text" id="createDateTime" name="createDateTime" readonly="readonly" value='${data.createDateTime}'/>
		</div>
	</div>
	<div class="form-actions">
		<a id="saveButton" href="javascript:;" class="btn btn-primary" onclick="saveDetail()"><i class="icon-ok"></i>保存</a>
	</div>
</fieldset>

</form> 

</div>

<%@ include file="../../../footer.jsp"%>
